﻿using Microsoft.Win32;
using Mono.Cecil;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace FirstTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void SelectFolder_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog
            {
                Filter = "DLL or EXE Files (*.dll;*.exe)|*.dll;*.exe",
                Title = "选择 DLL 文件"
            };

            if (dialog.ShowDialog() == true)
            {
                string filePath = dialog.FileName;
                string xmlFilePath = Path.ChangeExtension(filePath, "xml");

                if (File.Exists(xmlFilePath))
                {
                    ExtractFileInfo(filePath, xmlFilePath);
                }
                else
                {
                    ExtractFileInfo(filePath, null);
                }
            }
        }

        private void ExtractFileInfo(string filePath, string xmlFilePath)
        {
            try
            {
                StringBuilder output = new StringBuilder();

                output.AppendLine($"处理文件: {filePath}");
                using (var assembly = AssemblyDefinition.ReadAssembly(filePath))
                {
                    foreach (var module in assembly.Modules)
                    {
                        output.AppendLine($"模块: {module.Name}");
                        foreach (var type in module.Types)
                        {
                            output.AppendLine($"  类: {type.FullName}");
                            foreach (var method in type.Methods)
                            {
                                output.AppendLine($"    方法: {method.Name}");
                                output.AppendLine($"      路径: {method.DeclaringType.FullName}.{method.Name}");
                                output.AppendLine($"      所属模块: {module.Name}");

                                if (method.HasParameters)
                                {
                                    output.AppendLine("      输入参数:");
                                    foreach (var parameter in method.Parameters)
                                    {
                                        output.AppendLine($"        {parameter.Name}: {parameter.ParameterType.FullName}");
                                    }
                                }

                                if (method.ReturnType != null)
                                {
                                    output.AppendLine($"      输出参数: {method.ReturnType.FullName}");
                                }

                                //string methodComment = GetMethodComment(assembly, method);
                                //if (!string.IsNullOrEmpty(methodComment))
                                //{
                                //    output.AppendLine("      方法注释:");
                                //    output.AppendLine($"        {methodComment}");
                                //}
                                //else
                                //{
                                //    output.AppendLine("      方法注释: 无");
                                //}
                            }
                        }
                    }
                }

                if (xmlFilePath != null && File.Exists(xmlFilePath))
                {
                    output.AppendLine($"处理 XML 文件: {xmlFilePath}");
                    string xmlContent = File.ReadAllText(xmlFilePath);
                    output.AppendLine(xmlContent);
                }

                OutputTextBox.Text = output.ToString();
            }
            catch (Exception ex)
            {
                OutputTextBox.Text = $"发生错误: {ex.Message}";
            }
        }

        //private string GetMethodComment(AssemblyDefinition assembly, MethodDefinition method)
        //{
        //    using (var stream = File.OpenRead(assembly.MainModule.FileName))
        //    {
        //        var reader = System.Reflection.Metadata.MetadataReaderProvider.FromPortablePdbStream(stream).GetMetadataReader();
        //        var methodHandle = reader.MethodDefinitions.FirstOrDefault(m => m. == method.Name && m.GetParameters().Count == method.Parameters.Count);

        //        if (methodHandle != default)
        //        {
        //            var methodDef = reader.GetMethodDefinition(methodHandle);
        //            var docCommentHandle = methodDef.GetDocumentationCommentId();

        //            if (docCommentHandle != default)
        //            {
        //                var docComment = reader.GetString(docCommentHandle);
        //                return docComment;
        //            }
        //        }
        //    }

        //    return null;
        //}
    }
}
